package cn.itcast.rabbitmq.service.impl;

import cn.itcast.rabbitmq.service.RabbitmqService;
import cn.itcast.util.MessageQueue;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author lyy
 * @Description TODO
 * @PACKAGE_NAME: cn.itcast.rabbitmq.service.impl
 * @ClassName RabbitmqServiceImpl
 * @Date 2020/7/2 9:14
 * @DAY_NAME_FULL: 星期四
 * @Version 1.0
 **/
@Service
public class RabbitmqServiceImpl implements RabbitmqService {

    private Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName());

    @Autowired
    private AmqpTemplate amqpTemplate;



    @Override
    public boolean send(String queue, Object object) {
        try {
            logger.info("发送消息队列   --- >Queue " + queue + "\t   消息===>" + object.toString());
            this.amqpTemplate.convertAndSend(queue,object.toString(), message -> {
                message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
                return message;
            });
            return true;
        }catch (Exception e){
            e.printStackTrace();
            logger.info("消息队列" + queue + "发送消息发生了异常" + object.toString());
            throw e;
        }
    }

    @Override
    public void sendTest(String msg) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("msg",msg);
        this.send(MessageQueue.QUEUE_USER_TEST,jsonObject);
    }

    /**
     * 功能描述: <br>
     * 〈用户申请提现通知〉
     * @Author: lyy
     * @Date: 2020/7/2 11:15
     * @Param: [openId, money]
     * @Return: void
     */
    @Override
    public void sendWithdrawRemind(String openId, String money) {
        JSONObject object = new JSONObject();
        object.put("openId",openId);
        object.put("money",money);
        this.send(MessageQueue.QUEUE_WITHDRAW_REMIND,object);
    }

    /**
     * 功能描述: <br>
     * 〈用户申请提现到账通知〉
     * @Author: lyy
     * @Date: 2020/7/2 14:10
     * @Param: [openId, money]
     * @Return: void
     */
    @Override
    public void sendWithdrawCash(String openId, String money) {
        JSONObject object = new JSONObject();
        object.put("openId",openId);
        object.put("money",money);
        this.send(MessageQueue.QUEUE_WITHDRAW_CASH,object);
    }


    /**
     * 功能描述: <br>
     * 〈提现审核不通过〉
     * @Author: lyy
     * @Date: 2020/7/3 10:08
     * @Param: [openId, money]
     * @Return: void
     */
    @Override
    public void WithdrawFailure(String openId, String money) {
        JSONObject object = new JSONObject();
        object.put("openId",openId);
        object.put("money",money);
        this.send(MessageQueue.QUEUE_WITHDRAW_FAILURE,object);
    }
}
